package com.unidt.www.module.third.doc;


import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.unidt.www.module.third.doc.domain.ProjectBusInfoDo;
import com.unidt.www.module.third.doc.domain.ProjectPlanDo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class WordTable {


    /**
     * 表头是3的时候
     */
    private  static  final int TABLE_TITLE_NUM=3;

    public static void main(String[]args) throws IOException {
        Document  doc = new Document();
        doc.loadFromFile("/Users/ouyangyansen/Desktop/shishi/2023舟山惠生海洋工程有限公司生产制造方式转型示范项目计划.wps");
        doc.saveToFile("/Users/ouyangyansen/Desktop/wordtohtml.html", FileFormat.Html);
        doc.dispose();

        List<Object> mListData= new ArrayList<>();
        List<ProjectPlanDo> mListProjectPlanDoData= new ArrayList<>();
        List<String> mlist= new ArrayList<>();

        String path = "/Users/ouyangyansen/Desktop/wordtohtml.html";
        File input = new File(path);
        org.jsoup.nodes.Document JsoupDoc = Jsoup.parse(input, "UTF-8");

        Element element  =JsoupDoc.select("table").first();

        ProjectBusInfoDo mProjectBusInfoDo=new ProjectBusInfoDo();
//        ProjectPlanDo mProjectPlanDo=new ProjectPlanDo();


        Elements els = element.select("tr");
        for (Element el : els) {
            Elements ele = el.select("td");
            for (int i=0;i< ele.size();i++) {
                if(ele.size()==1) {
                    /**
                     * 只有一行的表
                     */
                    mListData.add(ele.get(i).text());
//                    System.out.println("key:title"+",value:" +ele.get(i).text());
                }else if(ele.size()%2==0) {
                    /**
                     * 2行的倍数表
                     */
                    if(i%2==0) {
                        mListData.add(ele.get((i) < ele.size() ? i + 1 : 0).text());
//                        System.out.println("key:" + ele.get(i).text() + "，value:" + ele.get((i) < ele.size() ? i + 1 : 0).text());
                    }
                }else if(ele.size()%TABLE_TITLE_NUM==0) {
                    /**
                     * 3行的倍数表，暂时就表头是3行，超出3行还不知道怎么处理
                     */
                    if(i%TABLE_TITLE_NUM==0) {

                        /**
                         * 设置三个表头。
                         */
                        if(mlist.size()==0){
                            mlist.add(ele.get(i).text());
                            mlist.add(ele.get(i+1).text());
                            mlist.add(ele.get(i+2).text());
                        }else{
                            /**
                             * 给表头赋值
                             */
                                ProjectPlanDo mProjectPlanDo= new ProjectPlanDo();

                                for (int j=0;j<TABLE_TITLE_NUM;j++) {

                                        if(j==0){
                                            mProjectPlanDo.setPlanTime(ele.get(i+j).text());
                                        }else if(j==1){
                                            mProjectPlanDo.setInvestment(ele.get(i+j).text());
                                        }else if(j==2){
                                            mProjectPlanDo.setProgress(ele.get(i+j).text());
                                        }
//                                        System.out.println("key:" + mlist.get(i+j) + ",value:" + ele.get(i+j).text());

                            }
                            mListProjectPlanDoData.add(mProjectPlanDo);
                        }

                    }
                }
            }
        }
        for(int i=0;i< mListData.size();i++){

            System.out.println(i+"=================="+mListData.get(i));
        }
//
//        mProjectBusInfoDo.setEnterpriseName((String) mListData.get(1));
//        mProjectBusInfoDo.setEnterpriseNature((String) mListData.get(2));
//        mProjectBusInfoDo.setLegalRepresentative((String) mListData.get(3));
//        mProjectBusInfoDo.setAddress((String) mListData.get(4));
//        mProjectBusInfoDo.setEmployeesNumbers((String) mListData.get(5));
//        mProjectBusInfoDo.setEngineeringTechnicians((String) mListData.get(6));
//        mProjectBusInfoDo.setRegisteredCapital((String) mListData.get(7));
//        mProjectBusInfoDo.setTotalAssets((String) mListData.get(8));
//        mProjectBusInfoDo.setNetAssets((String) mListData.get(9));
//        mProjectBusInfoDo.setAnnualIncome((String) mListData.get(10));
//        mProjectBusInfoDo.setAnnualProfit((String) mListData.get(11));
//        mProjectBusInfoDo.setIndustryStatus((String) mListData.get(12));
//        mProjectBusInfoDo.setProjectManager((String) mListData.get(13));
//        mProjectBusInfoDo.setContacts((String) mListData.get(14));
//        mProjectBusInfoDo.setContactsTel((String) mListData.get(15));
//        /**
//         * 项目基本情况
//         */
//        mProjectBusInfoDo.setProjectName((String) mListData.get(17));
//        mProjectBusInfoDo.setProjectFilingCode((String) mListData.get(18));
//        mProjectBusInfoDo.setProjectTime((String) mListData.get(19));
//        mProjectBusInfoDo.setProjectTotalInvestment((String) mListData.get(20));
//        mProjectBusInfoDo.setProjectFixedInvestment((String) mListData.get(21));
//        mProjectBusInfoDo.setProjectWorkingCapital((String) mListData.get(22));
//        mProjectBusInfoDo.setNewSalesRevenue((String) mListData.get(23));
//        mProjectBusInfoDo.setAdditionalProfits((String) mListData.get(24));
//        mProjectBusInfoDo.setAdditionalExports((String) mListData.get(25));
//        mProjectBusInfoDo.setProjectAddress((String) mListData.get(26));
//
//        mProjectBusInfoDo.setTechnicalAnalysis((String) mListData.get(27));
//        mProjectBusInfoDo.setPerformanceObjectives((String) mListData.get(28));
//        mProjectBusInfoDo.setGovernmentFundingSupport((String) mListData.get(29));
//        mProjectBusInfoDo.setCommitmentOpinion((String) mListData.get(30));
//        /**
//         * table表
//         */
//        mProjectBusInfoDo.setPlanList(mListProjectPlanDoData);
//
//        String jsonString2= JSON.toJSONString(mProjectBusInfoDo);
//
//        System.out.println("jsonString2====:"+jsonString2);
    }





}
